package com.dji.activationDemo.messaging;

import com.dji.activationDemo.uitls.WriteLog;
import com.dji.activationDemo.websocket.WsManager;

import org.json.JSONException;
import org.json.JSONObject;

public class MessageProcessing {
    Simulator simulator = new Simulator();
    GimbalRotate gimbalRotate = new GimbalRotate();
    Camera camera = new Camera();
    WaypointMissionRes waypointMission = new WaypointMissionRes();
    HotpointMissionOperatorRes hotpointMissionOperatorRes = new HotpointMissionOperatorRes();
    AttitudeWaypointMission attitudeWaypointMission = new AttitudeWaypointMission();
    CustomWaypointMission customWaypointMission = new CustomWaypointMission();

    public void handleMesaage(String paramString) {
        String[] content = paramString.split("[*]");
        String sendName = content[0];
        String sign = content[1];
        try {
            String num = null;
//        String num = content[3].substring(0, 3);
//        System.out.println("sign:" + sign + ",num:" + num);
            String contentString = content[3];
            JSONObject contentJSON = null;
            if (contentString.contains("code")) {
                contentJSON = StringToJson(contentString);
                num = contentJSON.getString("code");
            } else {
                num = content[3].substring(0, 3);
            }

            switch (num) {
                case "001":
                    //起飞
                    simulator.startTakeoff(sign, sendName);
                    break;
                case "002":
                    //降落
                    simulator.startLanding(sign);
                    break;
                case "003":
                    //着陆
                    simulator.confirmLanding(sign);
                    break;
                case "004":
                    //启用虚拟棒
                    simulator.setVirtualStickModeEnabled(true, sign);
                    break;
                case "005":
                    //禁用虚拟棒
                    simulator.setVirtualStickModeEnabled(false, sign);
                    break;
                case "006":
                    //左虚拟棒控制飞行
                    //006,px,py
                    simulator.ScreenJoystickLeft(contentString, sign);
                    break;
                case "007":
                    //右虚拟棒
                    //007,px,py
                    simulator.ScreenJoystickRight(contentString, sign);
                    break;
                case "008":
                    //控制万向节
                    gimbalRotate.rotateGimbal(contentString, sign);
                    break;
                case "009":
                    //直播rtmp推流
                    camera.liveStream(sign);
                    break;
                case "010":
                    //停止rtmp推流
                    camera.stopLiveShow(sign);
                    //摄影测量航线任务飞行
                    //010|121.8710,31.5410,15|121.8720,31.5420,15|121.8720,31.5420,30|121.8710,31.5410,15
//                waypointMission.photogrammetryTask(contentString, sign);
                    break;
                case "011":
                    //摄影测量进阶版，俯视角-90
                    waypointMission.photogrammetryPitchTask1(contentString, sign, -90);
                    break;
                case "012":
                    //摄影测量进阶版，俯视角-45
                    waypointMission.photogrammetryPitchTask1(contentString, sign, -45);
                    break;
                case "013":
                    //返航
                    waypointMission.goHome(sign);
                    break;
                case "014":
                    //恢复航点任务
                    waypointMission.resumeMission(sign);
                    break;
                case "015":
                    //暂停航点任务
                    waypointMission.pauseMission(sign);
                    break;
                case "016":
                    //停止航点任务
                    waypointMission.stopMission(sign);
                    break;
                case "017":
                    //开始热点飞行
                    hotpointMissionOperatorRes.startHotpointMission(contentString, sign);
                    break;
                case "018":
                    //暂停热点飞行
                    hotpointMissionOperatorRes.pauseHotpointMission(sign);
                    break;
                case "019":
                    //恢复热点飞行
                    hotpointMissionOperatorRes.resumeHotpointMission(sign);
                    break;
                case "020":
                    //结束热点飞行
                    hotpointMissionOperatorRes.stopHotpointMission(sign);
                    break;
                case "021":
                    //拍照
                    camera.shootPhoto(sign, sendName);
                    break;
                case "022":
                    //开始录像
                    camera.startRecord(sign, sendName);
                    break;
                case "023":
                    //停止录像
                    camera.stopRecord(sign, sendName);
                    break;

                case "024":
                    //多次多角度拍照任务
                    attitudeWaypointMission.multitaskingFlight(contentString, sign, sendName);
                    break;

                case "025":
                    //录像测试
                    attitudeWaypointMission.waypointVideoDemo(contentString, sign, sendName);
                    break;

                case "026":
                    customWaypointMission.customFlight(contentJSON, sign, sendName);
                    break;
            }
        } catch (JSONException e) {
//            sendMessage(sendName + "*" + sign + "*0*{code:\"error\",data:{message:\"数据解析出错\"}}");
            e.printStackTrace();
            WriteLog.writeLog(sign+":数据解析报错："+e.getMessage());
        }

    }


    private JSONObject StringToJson(String contentString) {
        JSONObject contentJSON = null;
        try {
            contentJSON = new JSONObject(contentString);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return contentJSON;
    }


    //发送消息
    public void sendMessage(String message) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                boolean start = true;
                while (start) {
                    try {
                        if (WsManager.ws.isOpen()) {
                            System.out.println("发送成功：" + message);
                            WsManager.ws.sendText(message);
                            start = false;
                        }
                        Thread.sleep(250);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }).start();
    }
}
